﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using Entities;

namespace WindowsApp.QLGiaoVien
{
    public partial class frmAddGiaoVien : DevComponents.DotNetBar.Office2007Form
    {
        private GiaoVienManager _manager;
        private GiaoVien _giaovien;
        private bool _isUpdate;
        frmQLGiaoVien cha;
        public frmAddGiaoVien()
        {
            InitializeComponent();
        }

        public frmAddGiaoVien(GiaoVienManager _man,frmQLGiaoVien p, GiaoVien _mh=null) : this()
        {
            _manager = _man;
            _giaovien = _mh;
            cha = p;
            _isUpdate = (_mh != null);
        }
        private void frmGiaoVien_Load(object sender, EventArgs e)
        {
            if (_isUpdate)
            {
                txtMaGV.Text = _giaovien.MaGV;
                txtTenGV.Text = _giaovien.HoTen;
                txtChucVu.Text = _giaovien.ChucVu;
                txtHocVi.Text = _giaovien.HocVi;
                txtDiaChi.Text = _giaovien.DiaChi;
                mtbDienThoai.Text = _giaovien.DienThoai;
                txtEmail.Text = _giaovien.Email;
                 
            }
            txtMaGV.ReadOnly = _isUpdate;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!_isUpdate) _giaovien = new GiaoVien();

            _giaovien.MaGV = txtMaGV.Text;
            _giaovien.HoTen = txtTenGV.Text;
            _giaovien.ChucVu = txtChucVu.Text;
            _giaovien.HocVi = txtHocVi.Text;
            _giaovien.DiaChi = txtDiaChi.Text;
            _giaovien.DienThoai = mtbDienThoai.Text;
            _giaovien.Email = txtEmail.Text;

            

            var vResult = _manager.Validate(_giaovien);
            if (vResult.Valid)
            {
                var aResult = _manager.InsertOrUpdate(_giaovien);
                if (aResult.Success)
                {
                    cha.btnLamMoi.PerformClick();
                    MessageBox.Show((_isUpdate?"Cập nhật":"Thêm")+" thành công thông tin Giáo viên","Thông báo");
                    btnReset.PerformClick();
                }
                else
                    MessageBox.Show(aResult.GetErrorMessages());
            }
            else MessageBox.Show(vResult.ToString());
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtMaGV.Text = string.Empty;
            txtTenGV.Text = string.Empty;
            txtChucVu.Text = string.Empty;
            txtHocVi.Text = string.Empty;
            txtDiaChi.Text = string.Empty;
            mtbDienThoai.Text = string.Empty;
            txtEmail.Text = string.Empty;           
            this.Text = "Thêm giáo viên";
            txtMaGV.ReadOnly = _isUpdate;
        }
    }
}